package com.example.project.controller;

import com.example.project.dto.TcdmSecureHazardAssessmentDTO;
import com.example.project.entity.TcdmSecureHazardAssessment;
import com.example.project.entity.TcdmSecureHazardList;
import com.example.project.service.TcdmSecureHazardAssessmentService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * (TcdmSecureHazardAssessment)表控制层
 *
 * @author makejava
 * @since 2024-02-23 15:18:23
 */
@RestController
@CrossOrigin
@RequestMapping("tcdmSecureHazardAssessment")
public class TcdmSecureHazardAssessmentController {
    /**
     * 服务对象
     */
    @Resource
    private TcdmSecureHazardAssessmentService tcdmSecureHazardAssessmentService;

    /**
     * 分页查询
     * @param data type: TcdmSecureHazardAssessmentDTO
     * @return 查询结果
     */

    @PostMapping("findPage")
    public ResponseEntity<Page<TcdmSecureHazardAssessment>> queryByPage(@RequestBody TcdmSecureHazardAssessmentDTO data) throws ParseException {
        com.example.project.entity.Page page=data.getPage();
        Date end=null;
        Date star=null;
        if(data.getMyTime2().getEndTime()!=null&&data.getMyTime2().getEndTime()!=""){
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            end=format.parse(data.getMyTime2().getEndTime());
        }
        if(data.getMyTime2().getStartTime()!=null&&data.getMyTime2().getStartTime()!=""){
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            star =format.parse(data.getMyTime2().getStartTime());

        }
        return ResponseEntity.ok(this.tcdmSecureHazardAssessmentService.queryByPage(data.getTcdmSecureHazardAssessment(), PageRequest.of(page.getPage(), page.getSize()),star,end));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */

    @GetMapping("{id}")
    public ResponseEntity<TcdmSecureHazardAssessment> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.tcdmSecureHazardAssessmentService.queryById(id));
    }
    /**
     * 查询所有数据
     *
     * @return 所有数据
     */
    @GetMapping("findAll")
    public List<TcdmSecureHazardAssessment> findAll() {
        return tcdmSecureHazardAssessmentService.findList();
    }

    /**
     * 新增数据
     *
     * @param tcdmSecureHazardAssessment 实体
     * @return 新增结果
     */

    @PostMapping("add")
    public ResponseEntity<TcdmSecureHazardAssessment> add(@RequestBody TcdmSecureHazardAssessment tcdmSecureHazardAssessment) {
        return ResponseEntity.ok(this.tcdmSecureHazardAssessmentService.insert(tcdmSecureHazardAssessment));
    }

    /**
     * 编辑数据
     *
     * @param tcdmSecureHazardAssessment 实体
     * @return 编辑结果
     */

    @PutMapping("put")
    public ResponseEntity<TcdmSecureHazardAssessment> edit(@RequestBody TcdmSecureHazardAssessment tcdmSecureHazardAssessment) {
        return ResponseEntity.ok(this.tcdmSecureHazardAssessmentService.update(tcdmSecureHazardAssessment));
    }

    /**
     * 删除数据
     *
     * @param tcdmSecureHazardAssessment 主键
     * @return 删除是否成功
     */

    @DeleteMapping("del")
    public ResponseEntity<Boolean> deleteById(@RequestBody TcdmSecureHazardAssessment tcdmSecureHazardAssessment) {
        return ResponseEntity.ok(this.tcdmSecureHazardAssessmentService.deleteById1(tcdmSecureHazardAssessment.getTshaId()));
    }

//    @PostMapping("/findList")
//    public ResponseEntity<Page<TcdmSecureHazardList>> findList(@RequestBody TcdmSecureHazardAssessmentDTO data) {
//        com.example.project.entity.Page page=data.getPage();
//        return ResponseEntity.ok(this.tcdmSecureHazardAssessmentService.findlist(data.getTcdmSecureHazardAssessment(), PageRequest.of(page.getPage(), page.getSize()))) ;
//    }

}


